﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Config;
using Config.Enum;
using Newtonsoft.Json;
using PaymentBLL;
using WebTriple7.Class;
using WebTriple7.Constants;
using WebTriple7.Entity;
using WebTriple7.Master;

namespace WebTriple7.Pages
{
    public partial class Triple7GoVn : System.Web.UI.Page
    {
        private void GetAccessToken()
        {
            var valueCode = Request.QueryString["code"];
            var url = string.Empty;
            if (!string.IsNullOrEmpty(valueCode))
            {
                
                url = string.Format(Config.Global.Settings.GET_ACCESSTOKEN_URL, Config.Global.Settings.CLIENT_ID, valueCode,
                                   Config.Global.Settings.CLIENT_SECRET);

                var s = Library.Lib.SendRequest(url, "GET", null);
                

                var entity = JsonConvert.DeserializeObject<GoVnAccessTokenEntity>(s);
                //GoVnSession.SetGoAccessToken(entity.access_token);
                //GoVnSession.SetGoUserId(entity.UserId);
                //GoVnSession.SetGoUserName(entity.User);
                Utils.UserUtils.GoPlayAccessToken = entity.access_token;

                var ctr = new CtrAccount();
                string uName = entity.UserId;
                string fName = entity.User;
                var info = ctr.SelectAccountInfoByName(uName);
                int? id = 0;
                if (info != null)
                {
                    if (info.Status > 0)
                    {
                        AccountSession.Login(info.Id, uName, fName, (int)LoginType.GoVn);
                        
                    }
                    else
                    {
                        ltrMess.Text = "Tài khoản bị khóa";
                        
                    }
                }
                else
                {
                    ctr.InsertAccount(uName, fName, (int)LoginType.GoVn, fName, "", 1, ref id);
                    if (!(id > 0))
                    {
                        ltrMess.Text = "Lỗi tạo tài khoản";
                        
                    }
                    else
                    {
                        AccountSession.Login(id ?? 0, uName, fName, (int)LoginType.GoVn);
                        
                    }
                }                
                
            }
            else
            {
                throw new Exception("Không lấy được token vì không có code.");
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!Utils.UserUtils.IsLogin)
                {
                    
                        GetAccessToken();                    
                }
                if (Utils.UserUtils.IsLogin)
                {                    
                    ucHiloTriple71.Visible = true;
                    ucHiloTriple71.AccountId = Utils.UserUtils.AccountId;
                    ucHiloTriple71.UserName = Utils.UserUtils.UserName;
                    ucHiloTriple71.TypeAccount = Utils.UserUtils.TypeAccount;                    
                }
                else
                {
                    ucHiloTriple71.Visible = false;
                }

            }
            catch (Exception ex)
            {
                ucHiloTriple71.Visible = false;
                ltrMess.Text = "Lỗi kết nối với máy chủ";
            }

        }
    }
}